layui.use(['layer', 'table', 'form', 'laydate', 'element'], function(){
    let layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form
        , laydate = layui.laydate
        ,element = layui.element;

    //自定义验证规则
    form.verify({
        testerName: [
            /^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$/,
            '用户名不能包含特殊字符'
        ]
        ,testerPhone: [
            /\d{11}$/
            ,'请输入正确的手机号'
        ]
    });

    //监听提交
    form.on('submit(submit)', function(data){
        let suc = false;
        $.ajax({
            url: "/preLogin",
            data: JSON.stringify(data.field),
            type: "post",
            dataType:"json",
            async: false,
            contentType: 'application/json;charset=UTF-8',
            success: function (ret){
                if (ret.data){
                    suc = true;
                }else {
                    if(ret.count == -1){
                        $.ajax({
                            url: "/result/selectResult",
                            type: "get",
                            dataType:"json",
                            async: false,
                            success: function (d){
                                d = d.data;
                                let superiority = d.character.superiority;
                                superiority = superiority.replace(/\n/g, "<br>&emsp;&emsp;");
                                superiority = superiority.replace("作为个体", "<span>作为个体</span>");
                                superiority = superiority.replace("沟通特点", "<span>沟通特点</span>");
                                superiority = superiority.replace("作为朋友", "<span>作为朋友</span>");
                                superiority = superiority.replace("对待工作和事业", "<span>对待工作和事业</span>");
                                let over = d.character.over;
                                over = over.replace(/\n/g, "<br>&emsp;&emsp;");
                                over = over.replace("作为个体", "<span>作为个体</span>");
                                over = over.replace("沟通特点", "<span>沟通特点</span>");
                                over = over.replace("作为朋友", "<span>作为朋友</span>");
                                over = over.replace("对待工作和事业", "<span>对待工作和事业</span>");
                                let hue = d.character.hue;
                                let ch = '';
                                if (hue == "红色"){
                                    ch = "red-hue";
                                }else if(hue == "黄色"){
                                    ch = "yellow-hue";
                                }else if(hue == "蓝色"){
                                    ch = "blue-hue";
                                }else{
                                    ch = "green-hue";
                                }
                                //示范一个公告层
                                layer.open({
                                    type: 1
                                    ,title: false //不显示标题栏
                                    ,closeBtn: false
                                    ,area: ['800px', '630px']
                                    ,shade: 0.8
                                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                                    ,btn: ['知晓']
                                    ,btnAlign: 'c'
                                    ,moveType: 1 //拖拽模式，0或者1
                                    ,content: `
                                            <div>
                                                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                                                    <ul class="layui-tab-title">
                                                        <li class="layui-this">性格颜色</li>
                                                        <li>详细介绍</li>
                                                    </ul>
                                                    <div class="layui-tab-content">
                                                        <div class="layui-tab-item layui-show">
                                                            <div class="character-intro">
                                                                <p>您的性格颜色为：<span class="${ch}">${d.character.hue}</span></p>
                                                                <p>${d.character.intro.replace(/\n/g, "<br>")}</p>
                                                            </div>
                                                        </div>
                                                        <div class="layui-tab-item">
                                                            <div class="character-detail">
                                                                <div>
                                                                    <p>性格优势</p>
                                                                    <p>&emsp;&emsp;${superiority}</p>
                                                                </div>
                                                                <div>
                                                                    <p>性格过度</p>
                                                                    <p>&emsp;&emsp;${over}</p>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        `
                                });
                            }
                        });
                    }else{
                        layer.alert("手机号已存在", {icon: 1, title: "提示"}, function (i) {
                            layer.close(i);
                        });
                    }
                    suc = false;
                }
            }
        });
        return suc;
    });
});